<template>
  <div class="app">

    <main class="app-main">
      <div class="layout">
        <div class="sidebar">
          <TreeControls />
        </div>
        
        <div class="content">
          <TreeView />
        </div>
      </div>
    </main>

    <footer class="app-footer">
      <div class="footer-content">
        <button @click="exportTree" class="btn btn-outline">
          导出树数据
        </button>
        <button @click="importTree" class="btn btn-outline">
          导入树数据
        </button>
        <button @click="clearTree" class="btn btn-danger">
          清空树
        </button>
      </div>
    </footer>
  </div>
</template>

<script setup>
import { onMounted } from 'vue';
import { useTreeStore } from './stores/treeStore';
import TreeView from './components/TreeView.vue';
import TreeControls from './components/TreeControls.vue';

const treeStore = useTreeStore();

onMounted(() => {
  // 如果没有初始化树，自动初始化一个示例树
  if (!treeStore.tree) {
    treeStore.initializeTree({ name: '公司组织架构' });
    
    // 添加示例数据
    setTimeout(() => {
      const devDept = treeStore.addNode('root', { name: '技术部' });
      const salesDept = treeStore.addNode('root', { name: '销售部' });
      
      treeStore.addNode(devDept.id, { name: '前端团队' });
      treeStore.addNode(devDept.id, { name: '后端团队' });
      treeStore.addNode(devDept.id, { name: '运维团队' });
      
      treeStore.addNode(salesDept.id, { name: '国内销售' });
      treeStore.addNode(salesDept.id, { name: '国际销售' });
    }, 100);
  }
});

const exportTree = () => {
  const treeData = treeStore.exportTree();
  if (treeData) {
    const dataStr = JSON.stringify(treeData, null, 2);
    const dataBlob = new Blob([dataStr], { type: 'application/json' });
    
    const link = document.createElement('a');
    link.href = URL.createObjectURL(dataBlob);
    link.download = 'tree-data.json';
    link.click();
    
    URL.revokeObjectURL(link.href);
    alert('树数据已导出');
  }
};

const importTree = () => {
  const input = document.createElement('input');
  input.type = 'file';
  input.accept = '.json';
  
  input.onchange = (e) => {
    const file = e.target.files[0];
    if (file) {
      const reader = new FileReader();
      reader.onload = (e) => {
        try {
          const treeData = JSON.parse(e.target.result);
          treeStore.importTree(treeData);
          alert('树数据导入成功');
        } catch (error) {
          alert('导入失败: 文件格式错误');
        }
      };
      reader.readAsText(file);
    }
  };
  
  input.click();
};

const clearTree = () => {
  if (confirm('确定要清空整个树吗？此操作不可撤销。')) {
    treeStore.clearTree();
  }
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background-color: #f8f9fa;
  color: #333;
  line-height: 1.6;
}

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 30px 20px;
  text-align: center;
}

.app-header h1 {
  font-size: 2.5rem;
  margin-bottom: 8px;
  font-weight: 700;
}

.app-header p {
  font-size: 1.1rem;
  opacity: 0.9;
}

.app-main {
  flex: 1;
  padding: 30px 20px;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

.layout {
  display: grid;
  grid-template-columns: 350px 1fr;
  gap: 30px;
  height: 70vh;
}

@media (max-width: 1024px) {
  .layout {
    grid-template-columns: 1fr;
    height: auto;
  }
  
  .sidebar {
    order: 2;
  }
  
  .content {
    order: 1;
  }
}

.sidebar {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

.content {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.app-footer {
  background: #343a40;
  color: white;
  padding: 20px;
  text-align: center;
}

.footer-content {
  display: flex;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s ease;
  text-decoration: none;
  display: inline-block;
}

.btn-primary {
  background-color: #007bff;
  color: white;
}

.btn-primary:hover {
  background-color: #0056b3;
}

.btn-secondary {
  background-color: #6c757d;
  color: white;
}

.btn-secondary:hover {
  background-color: #545b62;
}

.btn-danger {
  background-color: #dc3545;
  color: white;
}

.btn-danger:hover {
  background-color: #c82333;
}

.btn-outline {
  background-color: transparent;
  border: 1px solid #dee2e6;
  color: #dee2e6;
}

.btn-outline:hover {
  background-color: #dee2e6;
  color: #343a40;
}
</style>